<html>
<head><meta charset="utf-8"><title>memcopy unaligned data · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html">memcopy unaligned data</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="223940321"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940321" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940321">(Jan 25 2021 at 18:32)</a>:</h4>
<p>I have a <code>&amp;[u8]</code> and I'd like to turn it into a <code>&amp;[u16]</code>. If the original slice is aligned properly I can just cast the original one to a pointer and then use <code>slice::from_raw_parts</code> but if the orignal slice isn't aligned on an addr that is <code>u16</code> friendly, I need to memcopy the data to somewhere what is properly aligned. What's my best option? Using std::alloc::alloc? Or should I just create a <code>Vec&lt;u16&gt;</code> and loop over the original slice in chunks, copying the the data over?</p>



<a name="223940591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940591">(Jan 25 2021 at 18:34)</a>:</h4>
<p>I think <code>[T]::align_to</code> would be useful: <a href="https://doc.rust-lang.org/std/primitive.slice.html#method.align_to">https://doc.rust-lang.org/std/primitive.slice.html#method.align_to</a></p>



<a name="223940606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940606">(Jan 25 2021 at 18:34)</a>:</h4>
<p>Maybe something like <code>data.chunks_exact(2).map(|chunk| u16::from_le_bytes(chunk.try_into().unwrap())).collect::&lt;Vec&lt;u16&gt;&gt;()</code>?</p>



<a name="223940613"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940613" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940613">(Jan 25 2021 at 18:34)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.align_to">https://doc.rust-lang.org/nightly/std/primitive.slice.html#method.align_to</a> will do the transmute in a safe way for you, but doesn't really help with reallocating to get it right</p>



<a name="223940667"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940667" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940667">(Jan 25 2021 at 18:35)</a>:</h4>
<p><code>align_to</code> won't give the correct result when it is unaligned.</p>



<a name="223940710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940710">(Jan 25 2021 at 18:35)</a>:</h4>
<p>The data is correct, it's just not aligned, so <code>align_to</code> isn't what I need</p>



<a name="223940738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940738">(Jan 25 2021 at 18:36)</a>:</h4>
<p>well - it might be most efficient to go via align_to</p>



<a name="223940743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940743">(Jan 25 2021 at 18:36)</a>:</h4>
<p>You could use <code>Cow::Borrowed</code> when <code>align_to</code> works out, and <code>Cow::Owned</code> to copy otherwise</p>



<a name="223940786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940786">(Jan 25 2021 at 18:36)</a>:</h4>
<p>I think <span class="user-mention" data-user-id="133247">@bjorn3</span>'s is the right answer if I want to only use safe Rust</p>



<a name="223940847"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940847" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940847">(Jan 25 2021 at 18:36)</a>:</h4>
<p><span class="user-mention silent" data-user-id="116122">simulacrum</span> <a href="#narrow/stream/122651-general/topic/memcopy.20unaligned.20data/near/223940738">said</a>:</p>
<blockquote>
<p>well - it might be most efficient to go via align_to</p>
</blockquote>
<p>Not sure what you mean</p>



<a name="223940850"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940850" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940850">(Jan 25 2021 at 18:36)</a>:</h4>
<p>ah, yeah, align_to actually will not quite work because if you're not aligned it will give you a bunch of "wrong" u16s</p>



<a name="223940865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> rylev <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940865">(Jan 25 2021 at 18:36)</a>:</h4>
<p>Exactly</p>



<a name="223940909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223940909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cuviper <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223940909">(Jan 25 2021 at 18:37)</a>:</h4>
<p>but if <code>align_to</code> does give you an empty head and tail, the middle is usable</p>



<a name="223941028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223941028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark Drobnak <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223941028">(Jan 25 2021 at 18:38)</a>:</h4>
<p>Wouldn't <code>align_to::&lt;u16&gt;</code> on a <code>&amp;[u8]</code> with an extra byte result in an empty head and a single element tail?</p>



<a name="223941112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223941112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223941112">(Jan 25 2021 at 18:39)</a>:</h4>
<p>It would, but if the start is not at a multiple of two, it would result in a non-empty head, but the first two bytes are expected to form a single <code>u16</code>.</p>



<a name="223941598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223941598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mark Drobnak <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223941598">(Jan 25 2021 at 18:43)</a>:</h4>
<p>Yep, <code>align_to</code> puts the extra in the head not the tail: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=fab53af840bb330745c0a835a57fe293">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=fab53af840bb330745c0a835a57fe293</a></p>



<a name="223942668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223942668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223942668">(Jan 25 2021 at 18:52)</a>:</h4>
<p>Depends on alignment.  Here's a counterexample where the extra byte is at the end: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=9690110475e5bd887c39fd5764a5b0ab">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=9690110475e5bd887c39fd5764a5b0ab</a></p>



<a name="223961557"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/memcopy%20unaligned%20data/near/223961557" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/memcopy.20unaligned.20data.html#223961557">(Jan 25 2021 at 21:17)</a>:</h4>
<p>Someone asked me to add this to bytemuck just last week: <a href="https://docs.rs/bytemuck/1.5.0/bytemuck/allocation/fn.pod_collect_to_vec.html">https://docs.rs/bytemuck/1.5.0/bytemuck/allocation/fn.pod_collect_to_vec.html</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>